package com.tyjc.unique.view.ssq;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;

import com.tyjc.unique.view.ZouShiOnDraw;
import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.List;


public class OnDrawCenter extends ZouShiOnDraw {
	
	private Paint bgPaint;
	private Paint linePaint;
	private Paint textPaint;
	private Paint zheXianPaint;
	
	private int color_bg_h;//灰
	private int color_bg_w;//白
	private int color_line;
	private int color_text1;
	private int color_zhexian;
	
	private int zi_cxcs;
	private int zi_pjlc;
	private int zi_zdyl;
	private int zi_zdlc;
	
	private Bitmap qiu_btm;
	private int startNum = 1;
	public int type = 0;//0红球，1篮球

	public OnDrawCenter(Context context) {
		super(context);
	}
	
	public OnDrawCenter(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	
	public OnDrawCenter(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}
	
	@Override
	protected void init(Context context) {
		super.init(context);
		color_bg_h = context.getResources().getColor(R.color.bgh_c);
		color_bg_w = context.getResources().getColor(R.color.bgb);
		color_line = context.getResources().getColor(R.color.shu_line);
		color_text1 = context.getResources().getColor(R.color.zi_c);
		zi_cxcs = context.getResources().getColor(R.color.cxcs);
		zi_pjlc = context.getResources().getColor(R.color.pjlc);
		zi_zdlc = context.getResources().getColor(R.color.zdlc);
		zi_zdyl = context.getResources().getColor(R.color.zdyl);
		color_zhexian = context.getResources().getColor(R.color.zhexian_lan);
		
		bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		bgPaint.setColor(color_bg_w);
		
		linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		linePaint.setColor(color_line);
		linePaint.setStrokeWidth(1);
		
		textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		textPaint.setColor(color_text1);
		textPaint.setTextSize(16 * screen_density);
		
		zheXianPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		zheXianPaint.setColor(color_zhexian);
		zheXianPaint.setStrokeWidth(5);
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawColor(Color.WHITE);
		Log.e("ondraw_center", "column= "+column);
		
		Rect rect;
		List<Rect> rects = new ArrayList<Rect>();
		List<Integer> haoMas = new ArrayList<Integer>();
		
		if (kj != null) {
			for (int i = 0; i < row; i++) {
				
				for (int j = 0; j < column; j++) {
					String text = kj[i][j]+"";
					int x0 = (int)(unitLength*j);
					int x1 = (int)(unitLength*(j+1));
					int y0 = (int)(unitLength*i);
					int y1 = (int)(unitLength*(i+1));
					rect = new Rect(x0, y0, x1, y1);
					
					//绘制背景颜色
					if (i % 2 != 0) {
						bgPaint.setColor(color_bg_h);
					}else {
						bgPaint.setColor(color_bg_w);
					}
					canvas.drawRect(rect, bgPaint);
					
					//绘制线条
					if (j != 0) {
						canvas.drawLine(x0, y0, x0, y1, linePaint);
					}
					
					if (isShowTongJi && i== row - 4) {
						linePaint.setStrokeWidth(2);
						canvas.drawLine(0, y0, totalWidth, y0, linePaint);
					}
					
					
					//绘制小球背景图片
					if (text.equals("0") && !isShowTongJi) {
						rects.add(rect);
						haoMas.add(j+startNum);
					}else{
						textPaint.setColor(color_text1);
					}
					
					//绘制遗漏值
					if (isShowYiLou) {
						drawText(canvas, textPaint, text, rect);
					}
					
					//绘制文字
					if (isShowTongJi) {
						if (i == row-4) {//出现次数
							textPaint.setColor(zi_cxcs);
						}else if (i== row-3) {//平均遗漏
							textPaint.setColor(zi_pjlc);
						}else if (i== row-2) {//最大遗漏
							textPaint.setColor(zi_zdyl);
						}else if (i== row-1) {//最大连出
							textPaint.setColor(zi_zdlc);
						}
						
						drawText(canvas, textPaint, text, rect);
					}
					
				}
				
			}
			
			//画球
			for (int k = 0; k < rects.size(); k++) {
				if (type == 1 && isShowZheXian && k < (rects.size()-1)) {
					
					zheXianPaint.setStrokeWidth((float)2.5);//1.0为正常粗细
					canvas.drawLine(rects.get(k).centerX(), rects.get(k).centerY(), rects.get(k+1).centerX(), rects.get(k+1).centerY(), zheXianPaint);
				}
				
				canvas.drawBitmap(qiu_btm, null, rects.get(k), bgPaint);
				textPaint.setColor(color_bg_w);
				if (haoMas.get(k) < 10) {
					
					drawText(canvas, textPaint, "0"+haoMas.get(k), rects.get(k));
				}else {
					drawText(canvas, textPaint, ""+haoMas.get(k), rects.get(k));
				}
			}
			
		}else{
			rect = new Rect(0, 0, (int)totalWidth, (int)totalHeight);
			drawText(canvas, textPaint, "暂无数据!", rect);
		}
	}
	
	public void setQiu_btm(int id) {
		this.qiu_btm = BitmapFactory.decodeResource(context.getResources(), id);
	}
	
	@Override
	protected void setTotalWH() {
		totalWidth = unitLength * column;
		totalHeight = unitLength * row;
		
		setMeasuredDimension((int)totalWidth, (int)totalHeight);
		this.invalidate();
	}

}
